import {
  login,
  logout,
  getInfo,
  smslogin
} from '@/api/login'
import {
  fetchSearch
} from '@/api/common'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  setStore,
  getStore,
  clearStore
} from "@/utils/common";
import {
  Base64
} from 'js-base64'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    searchList:[],
    searchWord:'',
    checkData:''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_SEARCH:(state,searchList)=>{
      state.searchList = searchList
    },
    SET_SEARCHWORD(state,searchWord){
      state.searchWord = searchWord
    },
    SET_CHECKDATA(state,checkData){
      state.checkData = checkData
    }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      const loginName = userInfo.loginName.trim()
      return new Promise((resolve, reject) => {
        login(loginName, userInfo.password).then(response => {
          const data = response.data
          let baseToken = "";
          baseToken = data.token;
          setStore('loginInfo', data);
          setToken(baseToken);
          commit('SET_NAME', data.loginName)
          commit('SET_TOKEN', baseToken)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    SmsLogin({
      commit
    }, userInfo) {
      const phone = userInfo.userName.trim()
      return new Promise((resolve, reject) => {
        smslogin(phone, userInfo.code).then(response => {
          const data = response.data
          let baseToken = "";
          baseToken = data.token;
          setStore('loginInfo', data);
          setToken(baseToken);
          commit('SET_NAME', data.loginName)
          commit('SET_TOKEN', baseToken)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.data
          commit('SET_ROLES', ['admin'])
          // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组['admin','edtor']
          // commit('SET_ROLES', data.roles)
          // } else {
          //   reject('getInfo: roles must be a non-null array !')
          // }
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({
      commit
    }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        clearStore()
        resolve()
      })
    },

    Search({commit},search){
      return new Promise(resolve=>{
        fetchSearch(search).then(res=>{
          commit('SET_SEARCH',res)
        })
      })
    },

    ProductAct({commit},index){
      console.log(`${index}这是index`)
      commit('SET_CHECKDATA',index)
    }
    
  }
}

export default user